#include "excelparse.h"

const char *tips[] = {
    "This tool is only used to parse excel  files, and is very basic, for reference only\n",
    "Usage is as follows: *.exe *.excel \n"};
#define BLOCK_SIZE 512
int8_t parseExcel(char *path)
{
    FILE *fp = NULL;
    fp = fopen(path, "rb");
    printf("parseExcel %s\n", path);

    if (fp == NULL)
    {
        printf("open file failed\n");
        return -1;
    }

    fseek(fp, 0, SEEK_SET);

    int32_t fileSize = ftell(fp);

    do
    {
        char *buf = (char *)malloc(BLOCK_SIZE);
        if (buf == NULL)
        {
            printf("malloc failed\n");
            break;
        }

        int32_t readSize = fread(buf, 1, BLOCK_SIZE, fp);
        if (readSize <= 0)
        {
            printf("read file failed\n");
            break;
        }

        printf("readSize %d\n", readSize);

        // 释放
        if (buf != NULL)
        {
            free(buf);
            buf = NULL;
        }

    } while (1);

    // 关闭文件
    if (fp != NULL)
    {
        fclose(fp);
        fp = NULL;
        printf("close file success\n");
    }

    return 0;
}
int main(int argc, char *argv[])
{
    printf("param count %d\n", argc);
    for (int i = 0; i < argc; i++)
    {
        printf("param[%d] %s\n", i, argv[i]);
    }

    if (argc < 2)
    {
        printf("%s", tips[0]);
        printf("%s", tips[1]);
        return 0;
    }

    if ((strcmp(argv[1], "help") == 0) || (strcmp(argv[1], "-h") == 0))
    {
        printf("%s", tips[0]);
        printf("%s", tips[1]);
        return 0;
    }

    parseExcel(argv[1]);
    return 0;
}
